package pro.lingwu.station.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pro.lingwu.station.dto.db.Driver;
import pro.lingwu.station.service.DriverService;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author @lingwu
 * @date created in 2021/11/16
 */
@Api(tags = "Driver")
@RestController
public class DriverController {

    private final DriverService driverService;

    public DriverController(DriverService driverService){
        this.driverService=driverService;
    }

    @ApiOperation(value = "司机注册")
    @PostMapping("/driver")
    public ResponseEntity<?> regDriver(@RequestBody @Valid Driver driver){
        driverService.addDriver(driver);
        return ResponseEntity.ok(driver);
    }

    @ApiOperation(value = "司机登录")
    @GetMapping("/driver/{account}")
    public ResponseEntity<?> driverSignIn(@PathVariable String account, String password, HttpServletRequest request){
        Driver driver = driverService.signIn(account, password, request);
        if (driver == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(driver);
    }

    @ApiOperation(value = "修改司机信息")
    @PutMapping("/driver/{account}")
    public ResponseEntity<?> updateDriver(@PathVariable String account,
                                          @RequestBody Driver driver){
        driver.setAccount(account);
        driverService.updateDriver(driver);
        return ResponseEntity.ok("ok");
    }

    @ApiOperation(value = "获取所有司机")
    @GetMapping("/driver")
    public ResponseEntity<?> getDrivers(int start, int size){
        return ResponseEntity.ok(driverService.getDrivers(start, size));
    }

    @ApiOperation(value = "删除司机")
    @DeleteMapping("/driver/{account}")
    public ResponseEntity<?> rmDriver(@PathVariable String account){
        driverService.rmDriver(account);
        return ResponseEntity.ok("ok");
    }



}
